﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;

namespace bmapi.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        [HttpPost,Route("api/values")]
        public HttpResponseMessage Get()
        {
            try
            {
                HttpPostedFile file = HttpContext.Current.Request.Files["file"];
                HttpPostedFileBase uploadfile = new HttpPostedFileWrapper(file) as HttpPostedFileBase;
                if (uploadfile == null)
                {
                    return AssistFun.toJson(new { status = 201, tip = "no:请选择文件" });
                }
                if (uploadfile.FileName == "")
                {
                    return AssistFun.toJson(new { status = 201, tip = "no:请选择文件,fileup:" + file.FileName });
                }

                string fileExt = Path.GetExtension(uploadfile.FileName);
                StringBuilder sbtime = new StringBuilder();
                sbtime.Append(DateTime.Now.Year).Append(DateTime.Now.Month).Append(DateTime.Now.Day).Append(DateTime.Now.Hour).Append(DateTime.Now.Minute).Append(DateTime.Now.Second);
                string dir = "/UploadFile/" + sbtime.ToString() + fileExt;
                string realfilepath = HttpContext.Current.Request.MapPath(dir);
                string readDir = Path.GetDirectoryName(realfilepath);
                if (!Directory.Exists(readDir))
                    Directory.CreateDirectory(readDir);

                uploadfile.SaveAs(realfilepath);
                return AssistFun.toJson(new { status = 201, tip = "ok:" + dir+ "路径:"+file.FileName+ "fileExt扩展名:"+ fileExt+ "sbtime上传时间:"+ sbtime});
            }
            catch (Exception ex)
            {
                return AssistFun.toJson(new { status = 201, tip = "no:" + ex.Message });
            }
        }

        // GET api/values/5
        public string Get(int id)
        {
            string sql = "select name from User_tb where power=1 and PLID=@plid";
            List<string> names = new List<string>();
            SQLCommand sc = new SQLCommand(sql, "@plid", 6);
            string name = sc.ExecuteScalar();
            if (name == null)
                return " ";
            else
                return name;
        }

        // POST api/values
        [HttpPost,Route("testup")]
        public HttpResponseMessage Post()
        {
            HttpPostedFile  obj = HttpContext.Current.Request.Files["obj"];
            HttpPostedFileBase uploadfile = new HttpPostedFileWrapper(obj) as HttpPostedFileBase;
            return AssistFun.toJson(new {status=200,tip= uploadfile.FileName+ "fihe:" } );   

            //try
            //{

            //    //HttpPostedFile file = HttpContext.Current.Request.Files["fileup"];
            //    //HttpPostedFileBase uploadfile = new HttpPostedFileWrapper(file) as HttpPostedFileBase;
            //    if (uploadfile == null)
            //    {
            //        return AssistFun.toJson(new { status = 201, tip = "no:请选择文件" });
            //    }
            //    if (uploadfile.FileName == "")
            //    {
            //        return AssistFun.toJson(new { status = 201, tip = "no:请选择文件,fileup:" + uploadfile.FileName.ToString() });
            //    }

            //    string fileExt = Path.GetExtension(uploadfile.FileName);
            //    StringBuilder sbtime = new StringBuilder();
            //    sbtime.Append(DateTime.Now.Year).Append(DateTime.Now.Month).Append(DateTime.Now.Day).Append(DateTime.Now.Hour).Append(DateTime.Now.Minute).Append(DateTime.Now.Second);
            //    string dir = "/UploadFile/" + sbtime.ToString() + fileExt;
            //    string realfilepath = HttpContext.Current.Request.MapPath(dir);
            //    string readDir = Path.GetDirectoryName(realfilepath);
            //    if (!Directory.Exists(readDir))
            //        Directory.CreateDirectory(readDir);
            //    var files = HttpContext.Current.Request.Files;
            //    string fileNewName = uploadfile.FileName.ToString();
            //    files[0].SaveAs(HttpContext.Current.Server.MapPath("~/Uploads/" + fileNewName));
            //    uploadfile.SaveAs(realfilepath);
            //    return AssistFun.toJson(new { status = 201, tip = "ok:" + dir + "路径:" + uploadfile.FileName.ToString() + "fileExt扩展名:" + fileExt + "sbtime上传时间:" + sbtime });
            //}
            //catch (Exception ex)
            //{
            //    return AssistFun.toJson(new { status = 201, tip = "no:" + ex.Message });
            //}
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }


    }
}
